package com.warm.redis.config;

import com.warm.redis.RedissonCacheService;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;
import org.noear.solon.data.cache.CacheService;
import org.noear.solon.serialization.jackson.JacksonRenderFactory;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.solon.RedissonSupplier;

/**
 * 缓存配置
 *
 * @author warm
 */
@Configuration
public class RedisConfig {

    @Bean
    public RedissonClient redissonClient(@Inject("${redisson}") RedissonSupplier supplier
            , @Inject JacksonRenderFactory factory) {
        RedissonClient redissonClient = supplier.get();
        redissonClient.getConfig().setCodec(new JsonJacksonCodec(factory.config()));
        return redissonClient;
    }

    /**
     * 使用redis缓存
     *
     * @param client
     * @return
     */
    @Bean(name = "cache1" , typed = true) // typed 表示可类型注入 //即默认
    public CacheService cache1(RedissonClient client, @Inject("${redisson.defSeconds}") int defSeconds) {
        return new RedissonCacheService(client, defSeconds);
    }

}
